package test.work;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import utils.SmallTool;

import java.io.File;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ChceckImage {
    public static void main(String[] args) {


        checkImage();

    }

    @SneakyThrows
    public static void checkImage() {
        String rootPath = "D:\\pubmed\\";
        List<File> files = Arrays.asList(FileUtil.ls(rootPath));
        // 获取表的数据进行单个比对
        String db = "pmiddoidown_gcr_error";
        String sql = "select MAX(pmid) as max ,MIN(pmid) as min  from " + db;
        Db use = Db.use();
        Entity query = use.queryOne(sql);
        Integer max = query.getInt("max");
//        IntStream.rangeClosed(min, max).forEach(v -> {
//
//
//                }
//        );
        int breakSize = 1000;
        int page = max / breakSize + 1;
        SmallTool.printMess("最大值+++" + max);
        SmallTool.printMess("页码+++" + page);
        for (int i = 1; i <= page; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = breakSize * i;
            SmallTool.printMess("开始页码+" + i + "+++" + start + "++++end+++" + end);
            String querySql = "select * from " + db + " where pmid between " + start + " and " + end + " and status = 0";
            List<Entity> queryS = use.query(querySql);
            ExecutorService executors = Executors.newFixedThreadPool(20);
            if (queryS != null) {
                for (Entity entity : queryS) {
                    executors.submit(() -> {
                        Integer pmid = entity.getInt("pmid");
                        String figure = entity.getStr("figure");
                        if (StringUtils.isNotEmpty(figure) && !figure.equals("[]")) {
                            JSONArray strings = JSONArray.parseArray(figure);
                            List<String> urls = new LinkedList<>();
                            for (Object string : strings) {
                                JSONObject sin = (JSONObject) string;
                                Set<String> strings1 = sin.keySet();
                                String url = "";
                                for (String s : strings1) {
                                    url = s;
                                }
                                // 进行比对
                                urls.add(url);
                            }
                            // 预处理位置 进行分组放下
                            String folderNameFront = pmid / 50000 + 1 + "";
                            String folderNameLast = pmid / 50000 + 2 + "";
                            String newPath = rootPath + folderNameFront + "-" + folderNameLast + "\\" + pmid + "\\";
                            if (!FileUtil.exist(newPath)) {
                                Entity param = new Entity();
                                param.setTableName(db);
                                param.set("pmid", pmid);
                                entity.set("status", 2);
                                try {
                                    use.update(entity, param);
                                } catch (SQLException throwables) {
                                    throwables.printStackTrace();
                                }
                            } else {
                                boolean isOK = true;
                                T:
                                for (String url : urls) {
                                    String fileName = getFileName(url);
                                    if (!FileUtil.exist(newPath + fileName)) {
                                        isOK = false;
                                        break T;
                                    }

                                }
                                if (isOK) {
                                    Entity param = new Entity();
                                    param.setTableName(db);
                                    param.set("pmid", pmid);
                                    entity.set("status", 1);
                                    try {
                                        use.update(entity, param);
                                    } catch (SQLException throwables) {
                                        throwables.printStackTrace();
                                    }
                                } else {
                                    Entity param = new Entity();
                                    param.setTableName(db);
                                    param.set("pmid", pmid);
                                    entity.set("status", 2);
                                    try {
                                        use.update(entity, param);
                                    } catch (SQLException throwables) {
                                        throwables.printStackTrace();
                                    }
                                }
                            }

                        } else {
                            entity.set("status", 1);
                            Entity param = new Entity();
                            param.setTableName(db);
                            param.set("pmid", pmid);
                            try {
                                use.update(entity, param);
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }
                        }
                    });
                }
            }


            executors.shutdown();
            while (true) {
                if (executors.isTerminated()) {
                    SmallTool.printMess("全部比对结束了开始输出结果");
                    break;
                } else {
                    SmallTool.printMess("等待中开始页码比对++" + i + "页++++++++++++++++");
                    Thread.sleep(1200);

                }
            }
        }
    }

    //判断存在?
    public static String getFileName(String url) {
        String filePath = url.split("/")[url.split("/").length - 1];
        filePath = delPointStr(filePath);
        return filePath;
    }

    public static String delPointStr(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        if (str.trim().endsWith(".")) {
            str = str.substring(0, str.length() - 1).trim();
            if (str.endsWith(".")) {
                str = delPointStr(str);
            }
        }

        // windows 文件夹特殊字符
        // \ / : * ? " < > |

        if (str.contains("\\")) {
            str = str.replace("\\", "");
        }
        if (str.contains("/")) {
            str = str.replace("/", "");
        }
        if (str.contains(":")) {
            str = str.replace(":", "");
        }
        if (str.contains("*")) {
            str = str.replace("*", "");
        }
        if (str.contains("?")) {
            str = str.replace("?", "");
        }
        if (str.contains("\"")) {
            str = str.replace("\"", "");
        }
        if (str.contains("<")) {
            str = str.replace("<", "");
        }
        if (str.contains(">")) {
            str = str.replace(">", "");
        }
        if (str.contains("|")) {
            str = str.replace("|", "");
        }
        return str;
    }
}
